﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using System.Collections;
using Microsoft.LightSwitch;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;
using System.Data;

namespace LightSwitchApplication
{
    public class PlacesController : BasicController
    {
        // GET api/<controller>
        public object Get() //IEnumerable<string>
        {
            //var serializer = new JavaScriptSerializer();
            //var data = from c in DataWorkspace.MMC_Content.Places.GetQuery().Execute() select c.Name; //serializer.Serialize(c)
            var result = (from p in DataWorkspace.MMC_Content.Places.GetQuery().Execute()
                          select new { Id = p.Id ,Name = p.Name, Latitude = p.Latitude, Longitude = p.Longitude, Address = p.Address });
            //string jsonString = serializer.Serialize(data);
            return result;
        }

        // GET api/<controller>/5
        public object Get(int id)
        {
            return (from p in DataWorkspace.MMC_Content.Places.GetQuery().Execute() 
                    where p.Id == id 
                    select new { Id = p.Id ,Name = p.Name, IsShared = p.IsShared
                        , Latitude = p.Latitude, Longitude = p.Longitude, Address = p.Address
                        , Image = p.Image })
                .FirstOrDefault();
        }

        // GET api/<controller>
        /// <summary>
        /// http://localhost:54565/api/Places?Longitude=138&Latitude=-35&MaxDistance=100000
        /// </summary>
        /// <param name="Longitude">decimal(22,15)</param>
        /// <param name="Latitude">decimal(22,15)</param>
        /// <param name="MaxDistance">1000 meters by default</param>
        /// <param name="MaxCount">10 by default</param>
        /// <param name="MaxPrice"></param>
        /// <param name="MaxHours"></param>
        /// <param name="MaxMinutes"></param>
        /// <param name="Date">Now by default</param>
        /// <param name="Occupied">Is place available as of Date</param>
        /// <returns></returns>
        public object Get(decimal Longitude, decimal Latitude, int? MaxDistance = 1000, int? MaxCount = 10, decimal? MaxPrice = 0, int? MaxHours = 0, int? MaxMinutes = 0, DateTime? Date = null, bool? Occupied = null) 
        {
            //if (Date == null) Date = DateTime.Now;
            //DataTable sqlData = SQLAccess.GetNearestPlaces(138.5041765449829M, -35.16708111198312M, 1000, 100, 0, 0, 0, DateTime.Now, false);
            DataTable sqlData = SQLAccess.GetNearestPlaces(Longitude, Latitude, MaxDistance, MaxCount, MaxPrice, MaxHours, MaxMinutes, Date, Occupied);
            var placesFound = from row in sqlData.AsEnumerable() select row;
            var result = (from p in DataWorkspace.MMC_Content.Places.GetQuery().Execute()
                          join pf in placesFound on p.Id equals (int)pf["Id"]
                      select new { Id = p.Id, Name = p.Name, Latitude = p.Latitude, Longitude = p.Longitude, Address = p.Address
                          , Distance = pf["Distance"].ToString(), MinMinutes = pf["MinMinutes"], MinPrice = pf["MinPrice"]
                          , OccupiedTo = pf["OccupiedTo"]
                      });
            /*
                IEnumerable<Request> requests = this.DataWorkspace.ApplicationData.Requests.GetQuery().Execute();
                    int[] projectIds = requests.Select(r => r.ProjectId).Distinct().ToArray();
                    query = query.Where(p => projectIds.Contains(p.Id));

             */
            return result;
        }


        //// POST api/<controller>
        //public void Post([FromBody]string value)
        //{
        //}

        //// PUT api/<controller>/5
        //public void Put(int id, [FromBody]string value)
        //{
        //}

        //// DELETE api/<controller>/5
        //public void Delete(int id)
        //{
        //    var place = DataWorkspace.MMC_Content.Places_SingleOrDefault(id);
        //    if (place != null)
        //    {
        //        place.Delete();
        //        DataWorkspace.MMC_Content.SaveChanges();
        //    }
        //}

    }
}